<include file='Index/header' />
<div class="row">
	<br>
	<div class="col-sm-12" >
    	<button type="button" data-toggle="modal" class="btn btn-primary" onclick="edit()">新建A类仓库</button>
    	<br><br>
   </div>
	
	<div class="col-sm-12">
	<table id="a-repository-table" class="table table-hover table-bordered table-striped table-striped">
		<thead>
			<tr>
				<td>编号</td>
				<td>仓库名称</td>
				<td>仓库地址</td>
				<td>联系人</td>
				<td>联系电话</td>
				<td style="width:21%">操作</td>
			</tr>
		</thead>
		<tbody>
			
		</tbody>
	</table>
	</div>
	<br>
</div>
<include file='Index/footer' />
<include file="Repository/a-repository-detail" />
<script>
$(document).ready(function() {
		loadTable();	
		loadBrepository();
});

function loadTable(){
	var table = $('#a-repository-table').DataTable({
		"bProcessing": true,
		"bDestroy": true,
		"sPaginationType" : "input",
		"oLanguage" : {
              "sLengthMenu": "每页显示 _MENU_ 条记录",
              "sZeroRecords": "抱歉， 没有找到",
              "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
              "sInfoEmpty": "没有数据",
              "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
              "sZeroRecords": "没有检索到数据",
               "sSearch": "搜索: &nbsp",
              "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
              }
          }, 
		"ajax":'{:U("Admin/Repository/getARepository/")}',
		"columns": [
	        { "data": 'id' },
	        { "data": 'name' },
	        { "data": 'position' },
	        { "data": 'linkman' },
	        { "data": 'phone' },
	        { "data": 'btn' }
    ],
    "columnDefs": [
    {
    	"targets": 0,
    	"render": function (data, type, row, table) {
        return table.row + 1;
      }
    },
    {
    	"targets": -1,
    	"data": null,
    	"render": function (data, type, row, table) {
    		var btn = '暂停下单';
    		var status = '1';
    		if(row.status != '0'){
					btn = '开始下单';
					status = '0';
    		}
        return '<button class="btn btn-info" onclick="edit(\''+row.a_repository_id+'\')">编辑</button>  <button class="btn btn-danger" onclick="del(\''+row.a_repository_id+'\')">删除</button>  <button class="btn btn-warning" onclick="modStatus(\''+row.a_repository_id+'\',\''+status+'\')">'+btn+'</button>';
      },
    }]
	});
}

function edit(a_repository_id){
	loadBrepository();

	//编辑按钮点击，弹出编辑框
	$.ajax({
			url: '{:U("Admin/Repository/getARepository/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"a_repository_id":a_repository_id
			}
		})
		.success(function(data){
			$("#b_repository_id [value="+data.b_repository_id+"]").attr("selected",'selected');
			$("input[name='a_repository_id']").attr('value', data.a_repository_id);
			$("input[name='name']").attr('value', data.name);
			$("input[name='position']").attr('value', data.position);
			$("input[name='linkman']").attr('value', data.linkman);
			$("input[name='phone']").attr('value', data.phone);

			$('#a-repository-detail').modal('show');
	});
}

function del(a_repository_id){
	if(!confirm("确认删除该B仓库吗？")){
		return;
	}
	$.ajax({
			url: '{:U("Admin/Repository/deleteARepository/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"a_repository_id":a_repository_id
			}
		})
		.success(function(data){
			location.reload();
	});
}
function modStatus(a_repository_id, status){
	if(!confirm("是否确认？")){
		return;
	}
	$.ajax({
			url: '{:U("Admin/Repository/updateARepository/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"a_repository_id":a_repository_id,
				"status":status
			}
		})
		.success(function(data){
			location.reload();
	});
}

//获取B仓库列表
function loadBrepository(){
  $.ajax({
          url: '{:U("Admin/Repository/getBRepository/")}',
          type: 'get',
          dataType: 'json'
      })
      .success(function(data){
          var data = data.data;
          $('#b_repository_id').html('');
          for (var i = 0 ; i < data.length ; i++){
              var html = '<option value="' + data[i].b_repository_id + '">' + data[i].name + '</option>';
              $('#b_repository_id').append(html);
          }
  });
}

</script>
